package net.tsingyun.samples.mapper;

import static net.tsingyun.samples.mapper.SampleDsl.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.SqlBuilder.isIn;

import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import javax.annotation.Generated;
import net.tsingyun.commons.core.util.Maps;
import net.tsingyun.commons.mybatis.dsl.MybatisMapper;
import net.tsingyun.samples.domain.Sample;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.AliasableSqlTable;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper;
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper;
import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper;
import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;

@Mapper
@SuppressWarnings({"all"})
public interface SampleMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<Sample>, CommonUpdateMapper, MybatisMapper<Sample, Long> {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    BasicColumn[] selectList = BasicColumn.columnList(id, instanceId, orgCode, changeType, code, name, department, status, cuserId, ctime, uuserId, utime, sortId, deleteStatus, tenantId);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="SampleResult", value = {
        @Result(column="ID", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="INSTANCE_ID", property="instanceId", jdbcType=JdbcType.VARCHAR),
        @Result(column="ORG_CODE", property="orgCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="CHANGE_TYPE", property="changeType", jdbcType=JdbcType.BIGINT),
        @Result(column="CODE", property="code", jdbcType=JdbcType.VARCHAR),
        @Result(column="NAME", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="DEPARTMENT", property="department", jdbcType=JdbcType.VARCHAR),
        @Result(column="STATUS", property="status", jdbcType=JdbcType.TINYINT),
        @Result(column="CUSER_ID", property="cuserId", jdbcType=JdbcType.BIGINT),
        @Result(column="CTIME", property="ctime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="UUSER_ID", property="uuserId", jdbcType=JdbcType.BIGINT),
        @Result(column="UTIME", property="utime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="SORT_ID", property="sortId", jdbcType=JdbcType.INTEGER),
        @Result(column="DELETE_STATUS", property="deleteStatus", jdbcType=JdbcType.TINYINT),
        @Result(column="TENANT_ID", property="tenantId", jdbcType=JdbcType.BIGINT)
    })
    List<Sample> selectMany(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("SampleResult")
    Optional<Sample> selectOne(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default long count(net.tsingyun.commons.mybatis.dsl.CountCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, sample, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int delete(net.tsingyun.commons.mybatis.dsl.DeleteCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, sample, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int deleteByPrimaryKey(Long id_) {
         return delete(this.newDelete((c, where) ->
            c.where(id, isEqualTo(id_))
        ));
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insert(Sample row) {
        return MyBatis3Utils.insert(this::insert, row, sample, c ->
            c.map(id).toProperty("id")
            .map(instanceId).toProperty("instanceId")
            .map(orgCode).toProperty("orgCode")
            .map(changeType).toProperty("changeType")
            .map(code).toProperty("code")
            .map(name).toProperty("name")
            .map(department).toProperty("department")
            .map(status).toProperty("status")
            .map(cuserId).toProperty("cuserId")
            .map(ctime).toProperty("ctime")
            .map(uuserId).toProperty("uuserId")
            .map(utime).toProperty("utime")
            .map(sortId).toProperty("sortId")
            .map(deleteStatus).toProperty("deleteStatus")
            .map(tenantId).toProperty("tenantId")
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertMultiple(Collection<Sample> records) {
        return MyBatis3Utils.insertMultiple(this::insertMultiple, records, sample, c ->
            c.map(id).toProperty("id")
            .map(instanceId).toProperty("instanceId")
            .map(orgCode).toProperty("orgCode")
            .map(changeType).toProperty("changeType")
            .map(code).toProperty("code")
            .map(name).toProperty("name")
            .map(department).toProperty("department")
            .map(status).toProperty("status")
            .map(cuserId).toProperty("cuserId")
            .map(ctime).toProperty("ctime")
            .map(uuserId).toProperty("uuserId")
            .map(utime).toProperty("utime")
            .map(sortId).toProperty("sortId")
            .map(deleteStatus).toProperty("deleteStatus")
            .map(tenantId).toProperty("tenantId")
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertSelective(Sample row) {
        return MyBatis3Utils.insert(this::insert, row, sample, c ->
            c.map(id).toPropertyWhenPresent("id", row::getId)
            .map(instanceId).toPropertyWhenPresent("instanceId", row::getInstanceId)
            .map(orgCode).toPropertyWhenPresent("orgCode", row::getOrgCode)
            .map(changeType).toPropertyWhenPresent("changeType", row::getChangeType)
            .map(code).toPropertyWhenPresent("code", row::getCode)
            .map(name).toPropertyWhenPresent("name", row::getName)
            .map(department).toPropertyWhenPresent("department", row::getDepartment)
            .map(status).toPropertyWhenPresent("status", row::getStatus)
            .map(cuserId).toPropertyWhenPresent("cuserId", row::getCuserId)
            .map(ctime).toPropertyWhenPresent("ctime", row::getCtime)
            .map(uuserId).toPropertyWhenPresent("uuserId", row::getUuserId)
            .map(utime).toPropertyWhenPresent("utime", row::getUtime)
            .map(sortId).toPropertyWhenPresent("sortId", row::getSortId)
            .map(deleteStatus).toPropertyWhenPresent("deleteStatus", row::getDeleteStatus)
            .map(tenantId).toPropertyWhenPresent("tenantId", row::getTenantId)
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<Sample> selectOne(net.tsingyun.commons.mybatis.dsl.SelectCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList(), sample, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<Sample> select(net.tsingyun.commons.mybatis.dsl.SelectCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList(), sample, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<Sample> selectDistinct(net.tsingyun.commons.mybatis.dsl.SelectCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList(), sample, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<Sample> selectByPrimaryKey(Long id_) {
        return selectOne(this.newSelect((c, where) -> 
            c.where(id, isEqualTo(id_))
        ));
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int update(net.tsingyun.commons.mybatis.dsl.UpdateCompleter completer) {
        return MyBatis3Utils.update(this::update, sample, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateAllColumns(Sample row, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalTo(row::getId)
                .set(instanceId).equalTo(row::getInstanceId)
                .set(orgCode).equalTo(row::getOrgCode)
                .set(changeType).equalTo(row::getChangeType)
                .set(code).equalTo(row::getCode)
                .set(name).equalTo(row::getName)
                .set(department).equalTo(row::getDepartment)
                .set(status).equalTo(row::getStatus)
                .set(cuserId).equalTo(row::getCuserId)
                .set(ctime).equalTo(row::getCtime)
                .set(uuserId).equalTo(row::getUuserId)
                .set(utime).equalTo(row::getUtime)
                .set(sortId).equalTo(row::getSortId)
                .set(deleteStatus).equalTo(row::getDeleteStatus)
                .set(tenantId).equalTo(row::getTenantId);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateSelectiveColumns(Sample row, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalToWhenPresent(row::getId)
                .set(instanceId).equalToWhenPresent(row::getInstanceId)
                .set(orgCode).equalToWhenPresent(row::getOrgCode)
                .set(changeType).equalToWhenPresent(row::getChangeType)
                .set(code).equalToWhenPresent(row::getCode)
                .set(name).equalToWhenPresent(row::getName)
                .set(department).equalToWhenPresent(row::getDepartment)
                .set(status).equalToWhenPresent(row::getStatus)
                .set(cuserId).equalToWhenPresent(row::getCuserId)
                .set(ctime).equalToWhenPresent(row::getCtime)
                .set(uuserId).equalToWhenPresent(row::getUuserId)
                .set(utime).equalToWhenPresent(row::getUtime)
                .set(sortId).equalToWhenPresent(row::getSortId)
                .set(deleteStatus).equalToWhenPresent(row::getDeleteStatus)
                .set(tenantId).equalToWhenPresent(row::getTenantId);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKey(Sample row) {
         return update(this.newUpdate((c, where) ->
            c.set(instanceId).equalTo(row::getInstanceId)
            .set(orgCode).equalTo(row::getOrgCode)
            .set(changeType).equalTo(row::getChangeType)
            .set(code).equalTo(row::getCode)
            .set(name).equalTo(row::getName)
            .set(department).equalTo(row::getDepartment)
            .set(status).equalTo(row::getStatus)
            .set(cuserId).equalTo(row::getCuserId)
            .set(ctime).equalTo(row::getCtime)
            .set(uuserId).equalTo(row::getUuserId)
            .set(utime).equalTo(row::getUtime)
            .set(sortId).equalTo(row::getSortId)
            .set(deleteStatus).equalTo(row::getDeleteStatus)
            .set(tenantId).equalTo(row::getTenantId)
            .where(id, isEqualTo(row::getId))
        ));
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKeySelective(Sample row) {
         return update(this.newUpdate((c, where) ->
            c.set(instanceId).equalToWhenPresent(row::getInstanceId)
            .set(orgCode).equalToWhenPresent(row::getOrgCode)
            .set(changeType).equalToWhenPresent(row::getChangeType)
            .set(code).equalToWhenPresent(row::getCode)
            .set(name).equalToWhenPresent(row::getName)
            .set(department).equalToWhenPresent(row::getDepartment)
            .set(status).equalToWhenPresent(row::getStatus)
            .set(cuserId).equalToWhenPresent(row::getCuserId)
            .set(ctime).equalToWhenPresent(row::getCtime)
            .set(uuserId).equalToWhenPresent(row::getUuserId)
            .set(utime).equalToWhenPresent(row::getUtime)
            .set(sortId).equalToWhenPresent(row::getSortId)
            .set(deleteStatus).equalToWhenPresent(row::getDeleteStatus)
            .set(tenantId).equalToWhenPresent(row::getTenantId)
            .where(id, isEqualTo(row::getId))
        ));
    }

    @Override
    default Map<String, SqlColumn<?>> columnsMap() {
        return columns();
    }

    @Override
    default Map<String, String> columnMapping() {
        return mapping();
    }

    @Override
    default Map<String, String> fieldsMap() {
        return fields();
    }

    @Override
    default Map<String, String> excelsMap() {
        return excels();
    }

    @Override
    default List<Sample> selectByPrimaryKey(Collection<Long> ids) {
        return this.select(this.newSelect((dsl, where) -> dsl.where(id, isIn(ids))));
    }

    @Override
    default int deleteByPrimaryKey(Collection<Long> ids) {
        return this.delete(this.newDelete((c, where) -> c.where(id, isIn(ids))));
    }

    @Override
    default String table() {
        return "sys_demo";
    }

    @Override
    default String remarks() {
        return "简单示例";
    }

    @Override
    default AliasableSqlTable<?> sqlTable() {
        return sample;
    }

    @Override
    default SqlColumn<Long> idColumn() {
        return id;
    }

    @Override
    default BasicColumn[] selectList() {
        Map<String, String> extensibleFields = this.extensibleFields();
        if (Maps.isEmpty(extensibleFields)) {
            return selectList;
        }
        BasicColumn[] columns = new BasicColumn[extensibleFields.size() + selectList.length];
        AtomicInteger index = new AtomicInteger(0);
        for (BasicColumn basicColumn : selectList) {
            columns[index.getAndIncrement()] = basicColumn;
        }
        Set<Map.Entry<String, String>> entries = extensibleFields.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            columns[index.getAndIncrement()] = SqlColumn.of(entry.getValue(), this.sqlTable());
        }
        return columns;
    }

    @Override
    default Class<?> domainClass() {
        return Sample.class;
    }

    @Override
    default int insert(Sample row, Consumer<InsertDSL<Sample>> consumer) {
        return MyBatis3Utils.insert(this::insert, row, sample, c -> {
            InsertDSL<Sample> insertDsl = this.doInsertDsl(c);
            if (Objects.nonNull(consumer)) {
                consumer.accept(insertDsl);
            }
            return insertDsl;
        });
    }

    @Override
    default int insertMultiple(Collection<Sample> records, Consumer<MultiRowInsertDSL<Sample>> consumer) {
        return MyBatis3Utils.insertMultiple(this::insertMultiple, records, sample, c -> {
            MultiRowInsertDSL<Sample> insertDsl = this.doInsertMultipleDsl(c);
            if (Objects.nonNull(consumer)) {
                consumer.accept(insertDsl);
            }
            return insertDsl;
        });
    }

    @Override
    default int insertSelective(Sample row, Consumer<InsertDSL<Sample>> consumer) {
        return MyBatis3Utils.insert(this::insert, row, sample, c -> {
            InsertDSL<Sample> insertDsl = this.doInsertSelectiveDsl(row, c);
            if (Objects.nonNull(consumer)) {
                consumer.accept(insertDsl);
            }
            return insertDsl;
        });
    }

    default void newPrimaryKey(Sample row, AbstractWhereDSL<?> where) {
        where.and(id, isEqualTo(row::getId));
    }

    @Override
    default UpdateDSL<UpdateModel> updateAllColumnsDsl(Sample row, UpdateDSL<UpdateModel> dsl) {
        return updateAllColumns(row, dsl);
    }

    @Override
    default UpdateDSL<UpdateModel> updateSelectiveColumnsDsl(Sample row, UpdateDSL<UpdateModel> dsl) {
        return updateSelectiveColumns(row, dsl);
    }

    default InsertDSL<Sample> doInsertDsl(InsertDSL<Sample> c) {
        return c.map(id).toProperty("id")
                .map(instanceId).toProperty("instanceId")
                .map(orgCode).toProperty("orgCode")
                .map(changeType).toProperty("changeType")
                .map(code).toProperty("code")
                .map(name).toProperty("name")
                .map(department).toProperty("department")
                .map(status).toProperty("status")
                .map(cuserId).toProperty("cuserId")
                .map(ctime).toProperty("ctime")
                .map(uuserId).toProperty("uuserId")
                .map(utime).toProperty("utime")
                .map(sortId).toProperty("sortId")
                .map(deleteStatus).toProperty("deleteStatus")
                .map(tenantId).toProperty("tenantId");
    }

    default InsertDSL<Sample> doInsertSelectiveDsl(Sample row, InsertDSL<Sample> c) {
        return c.map(id).toPropertyWhenPresent("id", row::getId)
                .map(instanceId).toPropertyWhenPresent("instanceId", row::getInstanceId)
                .map(orgCode).toPropertyWhenPresent("orgCode", row::getOrgCode)
                .map(changeType).toPropertyWhenPresent("changeType", row::getChangeType)
                .map(code).toPropertyWhenPresent("code", row::getCode)
                .map(name).toPropertyWhenPresent("name", row::getName)
                .map(department).toPropertyWhenPresent("department", row::getDepartment)
                .map(status).toPropertyWhenPresent("status", row::getStatus)
                .map(cuserId).toPropertyWhenPresent("cuserId", row::getCuserId)
                .map(ctime).toPropertyWhenPresent("ctime", row::getCtime)
                .map(uuserId).toPropertyWhenPresent("uuserId", row::getUuserId)
                .map(utime).toPropertyWhenPresent("utime", row::getUtime)
                .map(sortId).toPropertyWhenPresent("sortId", row::getSortId)
                .map(deleteStatus).toPropertyWhenPresent("deleteStatus", row::getDeleteStatus)
                .map(tenantId).toPropertyWhenPresent("tenantId", row::getTenantId);
    }

    default MultiRowInsertDSL<Sample> doInsertMultipleDsl(MultiRowInsertDSL<Sample> c) {
        return c.map(id).toProperty("id")
                .map(instanceId).toProperty("instanceId")
                .map(orgCode).toProperty("orgCode")
                .map(changeType).toProperty("changeType")
                .map(code).toProperty("code")
                .map(name).toProperty("name")
                .map(department).toProperty("department")
                .map(status).toProperty("status")
                .map(cuserId).toProperty("cuserId")
                .map(ctime).toProperty("ctime")
                .map(uuserId).toProperty("uuserId")
                .map(utime).toProperty("utime")
                .map(sortId).toProperty("sortId")
                .map(deleteStatus).toProperty("deleteStatus")
                .map(tenantId).toProperty("tenantId");
    }

    @Override
    default UpdateDSL<UpdateModel> newUpdateDsl(Sample row, UpdateDSL<UpdateModel> c) {
        return c.set(id).equalTo(row::getId)
                .set(instanceId).equalTo(row::getInstanceId)
                .set(orgCode).equalTo(row::getOrgCode)
                .set(changeType).equalTo(row::getChangeType)
                .set(code).equalTo(row::getCode)
                .set(name).equalTo(row::getName)
                .set(department).equalTo(row::getDepartment)
                .set(status).equalTo(row::getStatus)
                .set(cuserId).equalTo(row::getCuserId)
                .set(ctime).equalTo(row::getCtime)
                .set(uuserId).equalTo(row::getUuserId)
                .set(utime).equalTo(row::getUtime)
                .set(sortId).equalTo(row::getSortId)
                .set(deleteStatus).equalTo(row::getDeleteStatus)
                .set(tenantId).equalTo(row::getTenantId);
    }

    @Override
    default UpdateDSL<UpdateModel> newUpdateSelectiveDsl(Sample row, UpdateDSL<UpdateModel> c) {
        return c.set(id).equalToWhenPresent(row::getId)
                .set(instanceId).equalToWhenPresent(row::getInstanceId)
                .set(orgCode).equalToWhenPresent(row::getOrgCode)
                .set(changeType).equalToWhenPresent(row::getChangeType)
                .set(code).equalToWhenPresent(row::getCode)
                .set(name).equalToWhenPresent(row::getName)
                .set(department).equalToWhenPresent(row::getDepartment)
                .set(status).equalToWhenPresent(row::getStatus)
                .set(cuserId).equalToWhenPresent(row::getCuserId)
                .set(ctime).equalToWhenPresent(row::getCtime)
                .set(uuserId).equalToWhenPresent(row::getUuserId)
                .set(utime).equalToWhenPresent(row::getUtime)
                .set(sortId).equalToWhenPresent(row::getSortId)
                .set(deleteStatus).equalToWhenPresent(row::getDeleteStatus)
                .set(tenantId).equalToWhenPresent(row::getTenantId);
    }
}